package com.kintreda.ieco_server.module.order.service.impl;

import com.kintreda.ieco_server.bean.goods.Items;
import com.kintreda.ieco_server.bean.goods.Waste;
import com.kintreda.ieco_server.bean.order.UsageRecord;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.order.service.IRecycleRecordService;
import com.kintreda.ieco_server.module.order.service.IRegenerantService;
import com.kintreda.ieco_server.module.order.service.IWasteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.service
 * @Author ZhongYao_H
 * @Date 2019/4/28:16:39
 * @Year 2019 Month 04 Day 28
 * @Date_zh 2019 年 四月 星期日
 */
@Service
public class WasteServiceImpl extends BaseServiceImpl<Waste,String> implements IWasteService {


    @Autowired
    IUserService userService;
    @Autowired
    IRecycleRecordService recycleRecordService;
    @Autowired
    IBaseRepository<UsageRecord,String> usagerecordRepository;
    @Autowired
    IRegenerantService itemsService;

    @Override
    public Object wasteDataByOrgId(String orgId) {
        List<Map<String, Object>> userIdList = usagerecordRepository.findListBySql("select id from common_user where orgId = ? ", new String[]{orgId});
        List<Items> types = itemsService.getType("1");
        for (Map<String, Object> map : userIdList) {
            String userId =  map.get("id").toString();
            List<Map<String, Object>> recylist = usagerecordRepository.findListBySql("select id  from recyclerecord where userId = ?", new Object[]{userId});
            for (Map<String,Object> recyMap : recylist){
                List<Waste> wastelist = getWasteByRecyId(recyMap.get("id").toString());
                if (wastelist!=null){
                    for (Items type : types) {
                        for (Waste waste : wastelist) {
                            if (waste.getTypeId().equals(type.getId())){
                                double typeNum = Double.parseDouble(type.getNum());
                                double wasteNum = Double.parseDouble(waste.getNum().toString());
                                typeNum = (wasteNum + typeNum);
                                type.setNum(typeNum+"");
                            }
                        }

                    }
                }

            }
        }

        for (Items type : types) {
            double typeNum = Double.parseDouble(type.getNum());
            typeNum = typeNum;
            type.setNum(formatToNumber(new BigDecimal(typeNum)));
        }


        return types;
    }

    @Override
    public Object wasteDataByUserId(String userId) {
        List<Items> types = itemsService.getType("1");
        List<Map<String, Object>> recylist = usagerecordRepository.findListBySql("select id  from recyclerecord where userId = ?", new Object[]{userId});
            for (Map<String,Object> recyMap : recylist){
                List<Waste>  wastes = getWasteByRecyId(recyMap.get("id").toString());
                if (wastes!=null){
                    for (Items type : types) {
                        for (Waste waste : wastes) {
                            if (waste.getTypeId().equals(type.getId())){
                                double typeNum = Double.parseDouble(type.getNum());
                                double wasteNum = Double.parseDouble(waste.getNum().toString());
                                typeNum = (wasteNum + typeNum);
                                type.setNum(typeNum+"");
                            }
                        }

                    }
                }
            }

        for (Items type : types) {
            double typeNum = Double.parseDouble(type.getNum());
            typeNum = typeNum;
            type.setNum(formatToNumber(new BigDecimal(typeNum)));
        }
        return types;

    }

    @Override
    public Object wasteDataAll() {
        List<Items> types = itemsService.getType("1");
        List<Map<String, Object>> typeNum = usagerecordRepository.findListBySql(" select sum(num) as num,typeId as type from waste GROUP BY typeId; ", null);
        for (Map<String, Object> waste : typeNum) {
            for (Items type : types) {
                if (StringUtils.equals(waste.get("type").toString(),type.getId())){
                    Double num = Double.parseDouble(waste.get("num").toString());
                    Double  total = Double.parseDouble(type.getNum());
                    total = (num + total);//单位转换为吨

                    type.setNum(formatToNumber(new BigDecimal(total)));
                    break;
                }
            }
        }

        return types;
    }

    @Override
    public Object wasteDataMax() {
        String[] sqls = {
           " select sum(w1.num) as num,w1.typeId as type from waste w1 WHERE (SELECT i.tyepC from items i where i.id = w1.typeId ) in ('0','1','2','3','4') GROUP BY w1.typeId; ",
           " select sum(w1.num) as num,w1.typeId as type from waste w1 WHERE (SELECT i.tyepC from items i where i.id = w1.typeId ) in ('5','6','7','8') GROUP BY w1.typeId; ",
           " select sum(w1.num) as num,w1.typeId as type from waste w1 WHERE (SELECT i.tyepC from items i where i.id = w1.typeId ) in ('9','10','13') GROUP BY w1.typeId; ",
           " select sum(w1.num) as num,w1.typeId as type from waste w1 WHERE (SELECT i.tyepC from items i where i.id = w1.typeId ) in ('11','12') GROUP BY w1.typeId; ",
        };

        Random random = new Random();
        int i = random.nextInt(sqls.length);
        List<Map<String, Object>> maps = usagerecordRepository.findListBySql(sqls[i], null);
        List<Items> types = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Object num = map.get("num");
            Object type = map.get("type");
            if (num !=null && type !=null){
                Items items = itemsService.get(type.toString());
                if (items!=null){
                    Double d = Double.parseDouble(num.toString());
                    items.setNum(d.toString());
                    types.add(items);
                }
            }
        }
        return types;
        }



    @Override
    public List<Waste> getWasteByRecyId(String recyId) {
        List<Waste> query = query("from Waste where sackIrecycleRecord = ? ", new Object[]{recyId});
        return query;
    }

    @Override
    public List<Waste> findByRecyId(String id) {
        List<Waste> query = query("from Waste where sackIrecycleRecord = ? ", new Object[]{id});
        return query;
    }


    public static String formatToNumber(BigDecimal obj) {
        DecimalFormat df = new DecimalFormat("#.00");
        if(obj.compareTo(BigDecimal.ZERO)==0) {
            return "0.00";
        }else if(obj.compareTo(BigDecimal.ZERO)>0&&obj.compareTo(new BigDecimal(1))<0){
            return "0"+df.format(obj).toString();
        }else {
            return df.format(obj).toString();
        }
    }
}
